<?php
require("sesionchk.php");
?>
<html>
<head>
<title>
<?php
require("title.php");
?>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php

include 'library/config.php';
include 'library/opendb.php';

// how many rows to show per page
$rowsPerPage = 3;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
	$pageNum = $_GET['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;

$query  = "SELECT FirstName,EmailAddress,ID,HomeStreet,MobilePhone FROM contacts ORDER BY ID DESC LIMIT $offset, $rowsPerPage";
$_GET[value] = "all";
/*
if ($_GET[value]=="all")
{
//$query  = "SELECT FirstName,EmailAddress,ID FROM contacts ORDER BY FirstName  ASC";
$query  = "SELECT `FirstName`,`HomeStreet`,`BusinessPhone`,`BusinessPhone2`,`BusinessFax`,`MobilePhone`,`HomePhone`,`HomePhone2`,`EmailAddress`,`Email2Address`,`Email3Address`,ID,user_id FROM contacts ORDER BY ID";
}
else
{
//$query  = "SELECT FirstName,EmailAddress,ID FROM contacts where FirstName like '".$_GET[value]."%' ORDER BY FirstName  ASC";
$query  = "SELECT `FirstName`,`BusinessStreet`,`BusinessPhone`,`BusinessPhone2`,`BusinessFax`,`MobilePhone`,`HomePhone`,`HomePhone2`,`EmailAddress`,`Email2Address`,`Email3Address`,ID,user_id FROM contacts where FirstName like '".$_GET[value]."%' ORDER BY FirstName  ASC";
}
*/
mysql_query("SET NAMES 'utf8'");
mysql_query('SET CHARACTER SET utf8'); 
$result = mysql_query($query) or die('Error, query failed');
?>
<table class="toptable"><tr>
<td class="leftlink"><a class="corr" href="logout.php" style="text-align:center">Logout</a></td><td class="rightlink"><a class="corr" href="#" style="text-align:center">About</a></td>
</tr>
</table>
<h1 class="cen">Address book</h1> 
<hr/>
<div class="cen">
	<table align="center">
	<tr>
	<th>Name</th><th>e-mail</th><th>Phone</th><th>Address</th><th>Photo</th><th>Action</th>
	</tr>
	<tr>
	<form id="form1" name="form1" method="post" action="search.php">
	<td><input type="text" name="txtSearch" /></td><td><input type="text" name="mail"></td><td><input type="text" name="phone"></td><td><input type="text" name="address"></td><td><input type="text" name="photo"></td><td><input type="submit" name="Submit" value="Find" /></td>
	</form>
	</tr>
	<tr>
	<form name="addnew" method="post">
	<td><input type="text" name="fullname" id="fullname"/></td><td><input type="text" name="txtemail"></td><td><input type="text" name="phmobile"></td><td><input type="text" name="addrhome"></td><td><input type="text" name="photo"><td><input type="submit" name="submit2" value="Add contact" onclick="form.action='saveaddr.php?a=1';" onsubmit="return checkForm();" /></td>
	</form>
	</tr>
	</table>
</div>
<h1 class="cen">Contacts</h1>

<table class="bottomtable">
<tr>
<td class="leftlink"><a class="corr" href="#">Prev</a></td><td class="rightlink"><a class="corr" href="#">Next</a></td>
<tr>
</table>

<table align="center" cellspacing="1" cellpadding="1">
  <tr>
    <td>
	<?php
	 
echo "<table width=520 cellpadding='1' cellspacing='1' bordercolor='#000000'> <COLGROUP span='10' width='0*'>";
?>
<tr>
<th>Name</th><th>e-mail</th><th>Phone</th><th>Address</th><th>Photo</th><th>Action</th>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
echo "<tr>";
	
	echo "<td>";
		
	 echo "<input type\"text\" value=\"" .$row['FirstName']. "\" />"; 
	
	echo "</td>";
	echo "<td>";
	
		
	 echo "<input type\"text\" value=\"" .$row['EmailAddress']. "\" />"; //echo "<a href='mailto:".trim($row['EmailAddress'])."'>" .trim($row['EmailAddress'])."</a>";
	
	echo "</td>";
	echo "<td>";
		
	 echo "<input type\"text\" value=\"" .$row['MobilePhone']. "\" />"; //echo "<b>".$row['MobilePhone']."</b>";
		
	echo "</td>";
	
	echo "<td>";
	 echo "<input type\"text\" value=\"" .$row['HomeStreet']. "\" />";
	echo "</td>";
	
	echo "<td>";
	
	$_GET['ID'] = $row['ID'];
	if ($_GET['ID']!==-1){
		//$query = "SELECT FirstName,EmailAddress,ID,Notes,imagename FROM contacts where ID=" .$_GET['ID'];
		$queryimg = "SELECT `imagename` , `imagedesc` FROM `tblimages` WHERE id =" .$_GET['ID'];
		mysql_query("SET NAMES 'utf8'");
		mysql_query('SET CHARACTER SET utf8'); 

		$imgresult = mysql_query($queryimg);
		//$row = mysql_fetch_array($imgresult, MYSQL_ASSOC);
		if (mysql_num_rows($imgresult)>0){
		echo "<table>";   //<tr><td></td><td></td></tr>";
		//echo "<tr>Click To Zoom</tr>";
		while($imgrow = mysql_fetch_array($imgresult))
		{
			if (trim($imgrow)!==""){
			echo "<tr>";						
			$p="uploads/".$imgrow['imagename'];
			$mw='100';
			$mh='70';
			if(list($w,$h) = @getimagesize($p)) {
			foreach(array('w','h') as $v) { $m = "m{$v}";
			if(${$v} > ${$m} && ${$m}) { $o = ($v == 'w') ? 'h' : 'w';
			$r = ${$m} / ${$v}; ${$v} = ${$m}; ${$o} = ceil(${$o} * $r); } }
			//echo "<center><img src='{$p}' alt='image' width='{$w}' height='{$h}'><br></center>";
			echo "<a href='showimage.php?a=".$imgrow['imagename']."&ID=".$_GET['ID']."&fn=s' target='_blank'><img src='uploads/".$imgrow['imagename']."' alt='Zoom In' width='{$w}' height='{$h}'></a>";
			}
			else{
			echo "<a href='showimage.php?a=".$imgrow['imagename']."&ID=".$_GET['ID']."&fn=s' target='_blank'><img src='uploads/".$imgrow['imagename']."' alt='Zoom In' height='70' width='100'></a>";
			}				//		echo "<a href='uploads/".$imgrow['imagename']."' target='_blank'><img src='uploads/".$imgrow['imagename']."' alt='Zoom In' height='70' width='100'></a>";
			echo "<br>";
			//echo "<tr>";
			$imgrowtmp =str_replace('.','',$imgrow['imagename']);
			//echo "<textarea name=\"". $imgrowtmp ."\" rows='4' style='width: 100px'>".$imgrow['imagedesc']."</textarea>";
			//	echo $imgrow['imagedesc'];
			echo "<br>";
			//echo "<tr>";
			echo "<a href='showimage.php?a=".$imgrow['imagename']."&ID=".$_GET['ID']."&fn=s&mod=del'>Delete</a>";
			//echo "<a href='library/delimg.php?ID=".$row['ID']."&nam=".$imgrow['imagename']."&FN=s'>Delete</a>";
			echo "<br>";			
			}				
		}				
		echo "</table>";				
		}
		else {
		echo "<a href=\"wabraaddressbook.php?ID=".$row['ID']."\"><div class=\"pic\"><img src=\"face.jpg\"></div></a>";
		}
	}	

	
	echo "</td>";
	echo "<td>";
	if ($_SESSION['usr_level'] <>3 or $_SESSION['usr_id']==$row['user_id'])
	{	
	echo "<a href=\"wabraaddressbook.php?ID=".$row['ID']."&salph=".$_GET[value]."&mod=del\">Delete</a>";
	}
	else
	{
	echo "&nbsp;";
	}
	//echo "<a href=\"library/delrec.php?ID=".$row['ID']."&salph=".$_GET[value]."\">Delete</a>";
	echo "</td>";
	
	echo "</tr>";
/*


	echo $row['FirstName'] ." <a href=\"wabraaddressbook.php?ID=".$row['ID']."\">Edit</a> &nbsp; <a href=\"library/delrec.php?ID=".$row['ID']."&salph=".$_GET[value]."\">Delete</a> <br>";
	*/
}
echo '</table>';
include 'library/closedb.php';
?>
	</td>
  </tr>
</table>

</body>
</html>